package pi.beta;

import cenas.EscolhaDeNiveis;
import cenas.FimDeJogo;
import cenas.MenuPrincipal;
import cenas.Nivel;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.extension.input.touch.controller.MultiTouchController;
import org.anddev.andengine.extension.input.touch.exception.MultiTouchException;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.view.KeyEvent;

public class Principal extends BaseGameActivity {
	private final int LARGURA_DA_RESOLUCAO = 800;
	private final int ALTURA_DA_RESOLUCAO = 480;

	public Camera camera;
	public Engine engine;
	private GerenciamentoDeCenasEEntrada trocaDeCenas;

	@Override
	public void onLoadComplete() {
	}

	@Override
	public Engine onLoadEngine() {
		camera = new Camera(0, 0, LARGURA_DA_RESOLUCAO, ALTURA_DA_RESOLUCAO);
		engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
			new RatioResolutionPolicy(LARGURA_DA_RESOLUCAO, ALTURA_DA_RESOLUCAO), camera).setNeedsMusic(true).setNeedsSound(true));
		try {
			engine.setTouchController(new MultiTouchController());
		} catch (MultiTouchException e) {
			e.printStackTrace();
		}
		return engine;
	}

	@Override
	public void onLoadResources() {
	}

	@Override
	public Scene onLoadScene() {
		trocaDeCenas = new GerenciamentoDeCenasEEntrada(this, this, getTextureManager(), getSoundManager(), getMusicManager());
		trocaDeCenas.cenaAtual = new MenuPrincipal(trocaDeCenas, this, 800, 480, getTextureManager(), getSoundManager());
		return trocaDeCenas.cenaAtual();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
	    if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
	        trocaDeCenas.ativaBotaoDireito();
	        return true;
	    }
	    if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
	        trocaDeCenas.ativaBotaoEsquerdo();
	        return true;
	    }
	    if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
	        trocaDeCenas.ativaBotaoPulo();
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
	        trocaDeCenas.desativaBotoesDirecionais();
	        return true;
	    }
		if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            trocaDeCenas.ativaBotaoPulo();
            trocaDeCenas.desativaBotaoPulo();
            return true;
        }
	    return super.onKeyUp(keyCode, event);
	}
	
	@Override
	public void onBackPressed() {
    	if(trocaDeCenas.cenaAtual() instanceof MenuPrincipal) {
    		finish();
    	}
    	if(trocaDeCenas.cenaAtual() instanceof EscolhaDeNiveis) {
    		trocaDeCenas.carregaCena(trocaDeCenas.MENU_PRINCIPAL);
    	}
    	if(trocaDeCenas.cenaAtual() instanceof Nivel) {
    		trocaDeCenas.carregaCena(trocaDeCenas.ESCOLHA_DE_NIVEIS);
    	}
    	if(trocaDeCenas.cenaAtual() instanceof FimDeJogo) {
    		trocaDeCenas.carregaCena(trocaDeCenas.MENU_PRINCIPAL);
    	}
	}
}